package hu.uszeged.inf.stud.prf.todo.dao.usertask;

import java.util.List;

import hu.uszeged.inf.stud.prf.todo.dao.core.GenericHibernateDAO;
import hu.uszeged.inf.stud.prf.todo.entity.task.Task;
import hu.uszeged.inf.stud.prf.todo.entity.user.User;
import hu.uszeged.inf.stud.prf.todo.entity.usertask.UserTask;

public class UserTaskDAO extends GenericHibernateDAO<UserTask, Long> {

	public UserTaskDAO() {
		super(UserTask.class);
	}

	@SuppressWarnings("unchecked")
	public List<User> getOtherUsersForTask(Task task) {
		StringBuilder query = new StringBuilder();
		query.append("from Task task ");
		query.append("where :task = task");
		return getSession().createQuery(query.toString())
				.setParameter("task", task)
				.list();	
	}
	
	@SuppressWarnings("unchecked")
	public List<UserTask> getUserTasksForUser(User user) {
		StringBuilder query = new StringBuilder();
		query.append("from UserTask usertask ");
		query.append("where usertask.user = :user and usertask.archive = :archive");
		return getSession().createQuery(query.toString())
				.setParameter("user", user)
				.setParameter("archive", false)
				.list();
	}
	
	public int getUserTodoCount(User user) {
		StringBuilder query = new StringBuilder();
		query.append("select count(*) from UserTask tt ");
		query.append("where tt.user = :user and tt.state = :todo");
		return ((Long) getSession().createQuery(query.toString())
				.setParameter("user", user)
				.setParameter("todo", false)
				.uniqueResult()).intValue();
	}
	
	public int getUserDoneCount(User user) {
		StringBuilder query = new StringBuilder();
		query.append("select count(*) from UserTask tt ");
		query.append("where tt.user = :user and tt.state = :todo");
		return ((Long) getSession().createQuery(query.toString())
				.setParameter("user", user)
				.setParameter("todo", true)
				.uniqueResult()).intValue();
	}
}
